#include "ModuleManager.hpp"
#include "Module.hpp"
#include "luaEventManager.hpp"

namespace lua
{

bool ModuleManager::add(Module *module)
{
	std::map<const char*, Module*>::iterator it = mModules.find(module->getName());
	if(it != mModules.end())
	{
		return false;
	}
	mModules[module->getName()] = module;
	return true;
}

bool ModuleManager::remove(Module *module)
{
	std::map<const char*, Module*>::iterator it = mModules.find(module->getName());
	if(it != mModules.end())
	{
		delete it->second;
		mModules.erase(it);
		return true;
	}
	return false;
}

Module* ModuleManager::getModuleFromName(const char *name)
{
	for(std::map<const char*, Module*>::iterator it = mModules.begin(); it != mModules.end();++it)
	{
		if(strcmp(name, it->first) == 0)
			return it->second;
	}
	return nullptr;
}

ModuleManager::~ModuleManager()
{
	delete EventManager::getInstance();
	for(std::map<const char*, Module*>::iterator it = mModules.begin(); it != mModules.end();)
	{
		delete it->second;
		it = mModules.erase(it);
	}
}

}